from rest_framework import serializers
from .models import *


class BookInfoModelSerializer(serializers.ModelSerializer):
    """图书 生成序列化器"""
    class Meta:
        model=BookInfo
        fields="__all__"

class HeroInfoSerializer(serializers.Serializer):
    """英雄 生成序列化器"""
    GENDER_CHOICES = (
        (0, 'female'),
        (1, 'male')
    )
    id = serializers.IntegerField(label='ID', read_only=True)
    hname = serializers.CharField(label='名称', max_length=20)
    hgender = serializers.ChoiceField(label='性别', choices=GENDER_CHOICES, default=0)
    hcomment = serializers.CharField(label='描述信息', max_length=200, allow_null=True, required=False)

    # 1、外键，关联的主键 read_only或queryset
    hbook = serializers.PrimaryKeyRelatedField(read_only=True)
    # hbook=serializers.PrimaryKeyRelatedField(queryset=BookInfo.objects.all())

    # 2、使用__str__返回值
    # hbook=serializers.StringRelatedField(read_only=True)

    # 3、关联书籍序列化器
    # hbook=BookInfoSerializer()
